package com.gold.bullion.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gold.bullion.R;

public class TabHostView extends LinearLayout {
	private Context context;
	private TextView textview;
	private ImageView image;
	private Drawable checkedIcon;
	private Drawable unCheckedIcon;

	public TabHostView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initContentView();
	}

	public TabHostView(Context context) {
		super(context);
		this.context = context;
		initContentView();
	}

	private void initContentView() {
		LayoutInflater.from(context).inflate(R.layout.tabhostcellview, this,
				true);
		textview = (TextView) this.findViewById(R.id.tabText);
		image = (ImageView) this.findViewById(R.id.tabIcon);

	}

	public void resize(int width, int height) {
		LinearLayout layout = (LinearLayout) this
				.findViewById(R.id.tabCellLayout);
		ViewGroup.LayoutParams param = layout.getLayoutParams();
		if (width > 0)
			param.width = width;
		if (height > 0)
			param.height = height;
		layout.setLayoutParams(param);
	}

	public void setTabCheckedIcon(Drawable drawable) {

		this.checkedIcon = drawable;
	}

	public void setTabUnCheckedIcon(Drawable drawable) {

		this.unCheckedIcon = drawable;
	}

	public void setText(String text) {
		textview.setTextColor(Color.WHITE);
		textview.setText(text);
	}

	public void isChecked(boolean check) {
		if (check) {
			this.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.tab_selected));
			image.setBackgroundDrawable(checkedIcon);

		} else {
			this.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.tab_unselected));
			image.setBackgroundDrawable(unCheckedIcon);
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
	
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			this.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.tab_press));
		}
			;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL: {
			this.setBackgroundDrawable(context.getResources().getDrawable(
					R.drawable.tab_selected));
		}
			;
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

}
